//
//  KeyBoardToolbar.m
//  KeepAccount
//
//  Created by Veer on 2017/12/21.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "KeyboardToolbar.h"

#import "NSString+Tools.h"

@interface KeyboardToolbar () <UITextFieldDelegate>


@end

@implementation KeyboardToolbar

//初始化
+ (instancetype)keyboardToolbar
{
    
    KeyboardToolbar *toolbar = [[KeyboardToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    return toolbar;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.userInteractionEnabled = YES;
        self.backgroundColor = RGB(250, 250, 250);
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    
    UILabel *noteLbl = [[UILabel alloc] init];
    noteLbl.text = @"备注：";
    noteLbl.font = VRFont(16);
    noteLbl.textColor = RGB(88, 88, 88);
    [self addSubview:noteLbl];
    [noteLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self);
        make.width.mas_lessThanOrEqualTo(48);
        make.height.mas_equalTo(40);
    }];
    
    
    UITextField *noteTF = [[UITextField alloc] init];
    noteTF.delegate = self;
    noteTF.returnKeyType = UIReturnKeyDone;
    noteTF.textColor = RGB(88, 88, 88);
    noteTF.font = VRFont(16);
    [self addSubview:noteTF];
    self.noteTF = noteTF;
    [noteTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(noteLbl.mas_right);
        make.centerY.mas_equalTo(self).offset(2);
        make.right.mas_equalTo(-140);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *amountLbl = [[UILabel alloc] init];
    amountLbl.text = @"0";
    amountLbl.font = VRFont(20);
    amountLbl.textColor = RGB(51, 51, 51);
    amountLbl.textAlignment = NSTextAlignmentRight;
    [self addSubview:amountLbl];
    self.amountLbl = amountLbl;
    [amountLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(noteTF.mas_right);
    }];
    
    // 线
    UIView *hLineView = [[UIView alloc] init];
    hLineView.backgroundColor = RGB(201, 201, 201);
    [self addSubview:hLineView];
    [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    self.text = @"0";
}

- (void)setText:(NSString *)text
{
    _text = text;
    
    // 处理数据
    NSString *lastStr = self.amountLbl.text;
    NSString *newStr = text;
    if ([self isPureFloat:newStr] && ![text hasSuffix:@"."]) {
        
        _text = [NSString moneyWith:[newStr doubleValue]];
        self.amountLbl.text = _text;
    }else if ([text hasSuffix:@"."]){
        
        _text = newStr;
        self.amountLbl.text = _text;
    }else if (text.length == 0){
        
        _text = @"0";
        self.amountLbl.text = _text;
    }else{
        
        _text = lastStr;
        self.amountLbl.text = lastStr;
    }
}

// 浮点形判断
- (BOOL)isPureFloat:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


#pragma mark - setter && getter
- (void)setNote:(NSString *)note
{
    _note = note;
    
    self.noteTF.text = note;
}


@end
